﻿using Painter.Model;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Media;

namespace Painter.Component
{
    public class BitmapCanvas:FrameworkElement
    {
        DrawingVisual mVisual=new DrawingVisual();
        Geometry mGeometry = new RectangleGeometry();
        Geometry mGeometryRegion = new RectangleGeometry();
        public void Add(DrawingVisual visual)
        {
            DrawingVisual newVisual = new DrawingVisual();
            newVisual.Copy(mVisual, visual);

            RemoveVisualChild(mVisual);
            AddVisualChild(newVisual);

            mVisual = newVisual;
            mGeometryRegion = new RectangleGeometry(newVisual.DescendantBounds);
        }
        protected override int VisualChildrenCount
        {
            get { return 1; }
        }
        protected override Visual GetVisualChild(int index)
        {
            return mVisual;
        }
        public DrawingVisual GetImage(Geometry geometry)
        {
            DrawingVisual drawing = new DrawingVisual();
            mGeometry = Geometry.Combine(mGeometryRegion, geometry, GeometryCombineMode.Exclude, null);

            mVisual.Clip = geometry;
            drawing.Copy(mVisual);
            mVisual.Clip = mGeometry;
            return drawing;
        }
    }
}
